home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Mail / sendmail.php < prev    next >
PHP Script  |  2004-10-01  |  5KB  |  134 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Chuck Hagenbuch <chuck@horde.org>                            |
  17. // +----------------------------------------------------------------------+
  18.  
  19. require_once 'Mail.php';
  20.  
  21. /**
  22.  * Sendmail implementation of the PEAR Mail:: interface.
  23.  * @access public
  24.  * @package Mail
  25.  * @version $Revision: 1.4 $
  26.  */
  27. class Mail_sendmail extends Mail {
  28.     
  29.     /**
  30.      * The location of the sendmail or sendmail wrapper binary on the
  31.      * filesystem.
  32.      * @var string
  33.      */
  34.     var $sendmail_path = '/usr/sbin/sendmail';
  35.     
  36.     /**
  37.      * Any extra command-line parameters to pass to the sendmail or
  38.      * sendmail wrapper binary.
  39.      * @var string
  40.      */
  41.     var $sendmail_args = '';
  42.     
  43.     /**
  44.      * Constructor.
  45.      * 
  46.      * Instantiates a new Mail_sendmail:: object based on the parameters
  47.      * passed in. It looks for the following parameters:
  48.      *     sendmail_path    The location of the sendmail binary on the
  49.      *                      filesystem. Defaults to '/usr/sbin/sendmail'.
  50.      *
  51.      *     sendmail_args    Any extra parameters to pass to the sendmail
  52.      *                      or sendmail wrapper binary.
  53.      *
  54.      * If a parameter is present in the $params array, it replaces the
  55.      * default.
  56.      *
  57.      * @param array $params Hash containing any parameters different from the
  58.      *              defaults.
  59.      * @access public
  60.      */    
  61.     function Mail_sendmail($params)
  62.     {
  63.         if (isset($params['sendmail_path'])) $this->sendmail_path = $params['sendmail_path'];
  64.         if (isset($params['sendmail_args'])) $this->sendmail_args = $params['sendmail_args'];
  65.  
  66.         /*
  67.          * Because we need to pass message headers to the sendmail program on
  68.          * the commandline, we can't guarantee the use of the standard "\r\n"
  69.          * separator.  Instead, we use the system's native line separator.
  70.          */
  71.         $this->sep = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  72.     }
  73.     
  74.     /**
  75.      * Implements Mail::send() function using the sendmail
  76.      * command-line binary.
  77.      * 
  78.      * @param mixed $recipients Either a comma-seperated list of recipients
  79.      *              (RFC822 compliant), or an array of recipients,
  80.      *              each RFC822 valid. This may contain recipients not
  81.      *              specified in the headers, for Bcc:, resending
  82.      *              messages, etc.
  83.      *
  84.      * @param array $headers The array of headers to send with the mail, in an
  85.      *              associative array, where the array key is the
  86.      *              header name (ie, 'Subject'), and the array value
  87.      *              is the header value (ie, 'test'). The header
  88.      *              produced from those values would be 'Subject:
  89.      *              test'.
  90.      *
  91.      * @param string $body The full text of the message body, including any
  92.      *               Mime parts, etc.
  93.      *
  94.      * @return mixed Returns true on success, or a PEAR_Error
  95.      *               containing a descriptive error message on
  96.      *               failure.
  97.      * @access public
  98.      */    
  99.     function send($recipients, $headers, $body)
  100.     {
  101.         $recipients = escapeShellCmd(implode(' ', $this->parseRecipients($recipients)));
  102.         
  103.         list($from, $text_headers) = $this->prepareHeaders($headers);
  104.         if (!isset($from)) {
  105.             return new PEAR_Error('No from address given.');
  106.         } elseif (strstr($from, ' ') ||
  107.                   strstr($from, ';') ||
  108.                   strstr($from, '&') ||
  109.                   strstr($from, '`')) {
  110.             return new PEAR_Error('From address specified with dangerous characters.');
  111.         }
  112.         
  113.         $result = 0;
  114.         if (@is_executable($this->sendmail_path)) {
  115.             $from = escapeShellCmd($from);
  116.             $mail = popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
  117.             fputs($mail, $text_headers);
  118.             fputs($mail, $this->sep);  // newline to end the headers section
  119.             fputs($mail, $body);
  120.             $result = pclose($mail) >> 8 & 0xFF; // need to shift the pclose result to get the exit code
  121.         } else {
  122.             return new PEAR_Error('sendmail [' . $this->sendmail_path . '] not executable');
  123.         }
  124.         
  125.         if ($result != 0) {
  126.             return new PEAR_Error('sendmail returned error code ' . $result);
  127.         }
  128.         
  129.         return true;
  130.     }
  131.     
  132. }
  133. ?>
  134.